const path = require('path')
const fastify = require('fastify')({ logger: true })
const fastifyStatic = require('fastify-static')

fastify.register(require('fastify-compress'), { global: true })

fastify.register(require('point-of-view'), {
    engine: {
        ejs: require('ejs')
    }
})

fastify.register(fastifyStatic, {
    root: path.join(__dirname, '../styles'),
    prefix: '/styles/',
    decorateReply: false,
})
fastify.register(fastifyStatic, {
    root: path.join(__dirname, '../scripts'),
    prefix: '/scripts/',
    decorateReply: false,
})
fastify.register(fastifyStatic, {
    root: path.join(__dirname, '../uploads'),
    prefix: '/uploads',
    decorateReply: false,
})
fastify.register(fastifyStatic, {
    root: path.join(__dirname, '../statics'),
    prefix: '/',
})

fastify.register(require('./controllers/blog'), { prefix: '/blog' })
fastify.register(require('./controllers/home'), { prefix: '/' })

// Run the server!
const start = async () => {
    try {
        await fastify.listen(3000)
    } catch (err) {
        fastify.log.error(err)
        process.exit(1)
    }
}
start()
